package com.zbc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zbc.dto.DishDTO;
import com.zbc.dto.DishPageDTO;
import com.zbc.entity.Dish;
import com.zbc.entity.Flavors;
import com.zbc.mapper.DishMapper;
import com.zbc.result.PageResult;
import com.zbc.service.DishService;
import com.zbc.vo.DishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult page(DishPageDTO dishPageDTO) {
        PageHelper.startPage(dishPageDTO.getPage(),dishPageDTO.getPageSize());
        Page<Dish> dishes = dishMapper.page(dishPageDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(dishes.getResult());
        pageResult.setTotal((int) dishes.getTotal());
        return pageResult;
    }

    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = Dish.builder()
                .name(dishDTO.getName())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .categoryId(dishDTO.getCategoryId())
                .description(dishDTO.getDescription())
                .status(dishDTO.getStatus())
                .build();
        dishMapper.addDish(dish);
        List<Flavors> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(flavors1 -> {
                flavors1.setDishId(dish.getId());
            });
            dishMapper.insertDishFlavors(flavors);
        }
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.updateDish(dish);
    }

    @Override
    public void deleteDish(Integer[] ids) {
        dishMapper.deleteDish(ids);
        dishMapper.deleteDishFlavors(ids);
    }

    @Override
    public DishVo fetchByIdDishInfo(Integer id) {
        Dish dish = dishMapper.getDishById(id);
        DishVo dishVo = DishVo.builder()
                .name(dish.getName())
                .id(dish.getId())
                .price(dish.getPrice())
                .image(dish.getImage())
                .description(dish.getDescription())
                .categoryId(dish.getCategoryId())
                .status(dish.getStatus())
                .flavors(dishMapper.getFlavorByDishId(id))
                .build();
        return dishVo;
    }

    @Override
    public void updateDishInfo(DishDTO dishDTO) {
        Dish dish = Dish.builder()
                .id(dishDTO.getId())
                .name(dishDTO.getName())
                .image(dishDTO.getImage())
                .price(dishDTO.getPrice())
                .status(dishDTO.getStatus())
                .categoryId(dishDTO.getCategoryId())
                .description(dishDTO.getDescription())
                .build();
        Integer[] ids = { dish.getId() };

        List<Flavors> flavors = dishDTO.getFlavors();

        dishMapper.updateDish(dish);

        dishMapper.deleteDishFlavors(ids);

        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });

            dishMapper.insertDishFlavors(flavors);
        }
    }
}
